package cn.edu.mju.auth.service.impl;

import cn.edu.mju.auth.mapper.IAuthUserInfoMapper;
import cn.edu.mju.auth.secu.LoginUser;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cn.edu.mju.common.entity.pojo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cjy
 * @data 2023/6/6 11:51
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {


    private final IAuthUserInfoMapper iAuthUserMapper;

    @Autowired
    public UserDetailsServiceImpl(IAuthUserInfoMapper iAuthUserMapper) {
        this.iAuthUserMapper = iAuthUserMapper;
    }


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        LambdaQueryWrapper<UserInfo> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UserInfo::getNickName, username);
        UserInfo systemUser = iAuthUserMapper.selectOne(lqw);

        if (ObjectUtils.isEmpty(systemUser)) {
            throw new UsernameNotFoundException("登录名或密码错误..");
        }
        List<String> auths = new ArrayList<>();
        auths.add("ADMIN");

        return new LoginUser(systemUser, auths);
    }



}
